<?php
/**
 * Created by PhpStorm.
 * User: Cui Jun
 * Date: 2016-11-9
 * Time: 16:32
 * To change this template use File | Settings | File Templates.
 */
namespace api\func;
use api\common\models\User;
use yii\web\HttpException;
class func{

    /*
     * sql安全过滤方法
     * @param $sql_str
     * @return int
     */
    public static function safe_sql($sql_str) {
        return preg_match ('/select|inert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|UNION|into|load_file|outfile/', $sql_str );
    }

    public static function safe_filter($str) {
        return strip_tags($str,'<p> <img>');
    }

    public static function getfirstchar($s0){
        $firstchar_ord=ord(strtoupper($s0{0}));
        if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0};
        $s=iconv("UTF-8","gb2312", $s0);
        $asc=ord($s{0})*256+ord($s{1})-65536;
        if($asc>=-20319 and $asc<=-20284)return "A";
        if($asc>=-20283 and $asc<=-19776)return "B";
        if($asc>=-19775 and $asc<=-19219)return "C";
        if($asc>=-19218 and $asc<=-18711)return "D";
        if($asc>=-18710 and $asc<=-18527)return "E";
        if($asc>=-18526 and $asc<=-18240)return "F";
        if($asc>=-18239 and $asc<=-17923)return "G";
        if($asc>=-17922 and $asc<=-17418)return "H";
        if($asc>=-17417 and $asc<=-16475)return "J";
        if($asc>=-16474 and $asc<=-16213)return "K";
        if($asc>=-16212 and $asc<=-15641)return "L";
        if($asc>=-15640 and $asc<=-15166)return "M";
        if($asc>=-15165 and $asc<=-14923)return "N";
        if($asc>=-14922 and $asc<=-14915)return "O";
        if($asc>=-14914 and $asc<=-14631)return "P";
        if($asc>=-14630 and $asc<=-14150)return "Q";
        if($asc>=-14149 and $asc<=-14091)return "R";
        if($asc>=-14090 and $asc<=-13319)return "S";
        if($asc>=-13318 and $asc<=-12839)return "T";
        if($asc>=-12838 and $asc<=-12557)return "W";
        if($asc>=-12556 and $asc<=-11848)return "X";
        if($asc>=-11847 and $asc<=-11056)return "Y";
        if($asc>=-11055 and $asc<=-10247)return "Z";
        return null;
    }

    /*
     * 表情符号过滤
     * @param $str 字符串
     * @param $title 提示
     * @param int $error 是否报错 1 报错 0 替换
     * @return mixed
     */
    public static function filterEmoji($str,$title,$error=1)
    {
        $str = preg_replace_callback(
            '/./u',
            function (array $match) use ($title,$error){
                //return strlen($match[0]) >= 4 ? '' : $match[0];
                if($error == 1){
                    if(strlen($match[0]) >= 4){
                        throw new HttpException(401,$title.' 请勿输入表情符号',0);
                    }else{
                        return $match[0];
                    }
                }else{
                    return strlen($match[0]) >= 4 ? '' : $match[0];
                }
            },
            $str);
        return $str;
    }

    /*
     * 替换手机号码
     * @param $phone
     * @return mixed
     */
    public static function replace_phone($phone){
        return substr_replace($phone, '****', 3, 4);
    }

    //消息极光推送
    public static function msg_push($phones,$alert,$msg){
        $url = 'http://120.27.160.189/jpush.php';
        $post_data = array(
            "platform"=>'all',
            "audience"=>'alias',
            "audience_data"=>$phones,
            "alert"=>$alert,
            'message'=>$msg,//{"title":"title","content":"content","content_type":"text","extras":{"type":"1","value":"1"}}
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch);
        if ($err) {
            echo "cURL Error #:" . $err;
            exit();
        }else{
            $_output = json_decode($output,1);
            if($_output['status'] != 'success'){
                //print_r($output);exit;
            }
        }
    }

    /**
     * curl 函数
     * @param string $url 请求的地址
     * @param string $type POST/GET/post/get
     * @param array $data 要传输的数据
     * @param string $err_msg 可选的错误信息（引用传递）
     * @param int $timeout 超时时间
     * @param array 证书信息
     * @author 勾国印
     */
    public static function GoCurl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array())
    {
        $url = \Yii::$app->params['tj_system_url'].$url;
        $type = strtoupper($type);
        if ($type == 'GET' && is_array($data)) {
            $data = http_build_query($data);
        }

        $option = array();

        if ( $type == 'POST' ) {
            $option[CURLOPT_POST] = 1;
        }
        if ($data) {
            if ($type == 'POST') {
                $option[CURLOPT_POSTFIELDS] = $data;
            } elseif ($type == 'GET') {
                $url = strpos($url, '?') !== false ? $url.'&'.$data :  $url.'?'.$data;
            }
        }

        $option[CURLOPT_URL]            = $url;
        $option[CURLOPT_FOLLOWLOCATION] = TRUE;
        $option[CURLOPT_MAXREDIRS]      = 4;
        $option[CURLOPT_RETURNTRANSFER] = TRUE;
        $option[CURLOPT_TIMEOUT]        = $timeout;

        //设置证书信息
        if(!empty($cert_info) && !empty($cert_info['cert_file'])) {
            $option[CURLOPT_SSLCERT]       = $cert_info['cert_file'];
            $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];
            $option[CURLOPT_SSLCERTTYPE]   = $cert_info['cert_type'];
        }

        //设置CA
        if(!empty($cert_info['ca_file'])) {
            // 对认证证书来源的检查，0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
            $option[CURLOPT_SSL_VERIFYPEER] = 1;
            $option[CURLOPT_CAINFO] = $cert_info['ca_file'];
        } else {
            // 对认证证书来源的检查，0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
            $option[CURLOPT_SSL_VERIFYPEER] = 0;
        }

        $ch = curl_init();
        curl_setopt_array($ch, $option);
        $response = curl_exec($ch);
        $curl_no  = curl_errno($ch);
        $curl_err = curl_error($ch);
        curl_close($ch);

        // error_log
        if($curl_no > 0) {
            if($err_msg !== null) {
                $err_msg = '('.$curl_no.')'.$curl_err;
            }
        }
        return $response;
    }

    public static function IdToUser($id){
        if($data = User::findOne(['id'=>$id])){
            return $data;
        }
        return false;
    }
}